package com.yuyou.train.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yuyou.train.service.api.user.dto.UserMedalRelationDTO;
import com.yuyou.train.user.service.IUserMedalRelationService;
import com.yuyou.train.user.vo.resp.RespUserMedal;
import com.yuyou.train.user.vo.resp.RespUserMedalTop;
import com.yuyou.train.util.bean.PageParams;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyou.train.service.api.user.dto.UserMedalItemDetailDTO;
import com.yuyou.train.service.api.user.entity.UserMedalItem;
import com.yuyou.train.user.mapper.UserMedalItemMapper;
import com.yuyou.train.user.service.IUserMedalItemService;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserMedalItemServiceImpl implements IUserMedalItemService{

	@Autowired
	private UserMedalItemMapper userMedalItemMapper;

	@Autowired
	private IUserMedalRelationService userMedalRelationService;
	
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userMedalItemMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(UserMedalItem record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		record.setIsUse("1");
		return userMedalItemMapper.insert(record);
	}

	@Override
	public UserMedalItemDetailDTO selectByPrimaryKey(String id) {
		return userMedalItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserMedalItem record) {
		record.setUpdateTime(new Date());
		return userMedalItemMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ResBoolSimpleInfo deleteUserMedalItem(String id) {
		ResBoolSimpleInfo res=new ResBoolSimpleInfo();
		//明细关联其他数据不能删除 TODO
		
		this.deleteByPrimaryKey(id);
		res.setSuccess(true);
		return res;
	}

	@Override
	public List<UserMedalItem> findItemBycategoryId(String medalCategoryId) {
		return userMedalItemMapper.findItemBycategoryId(medalCategoryId);
	}

	@Override
	public List<UserMedalItemDetailDTO> findUserMedalItemPageList(Pagination<UserMedalItemDetailDTO> p) {
		return userMedalItemMapper.findUserMedalItemPageList(p);
	}

	@Override
	public int findUserMedalItemPageCount(Pagination<UserMedalItemDetailDTO> p) {
		return userMedalItemMapper.findUserMedalItemPageCount(p);
	}

	@Override
	public Pagination<UserMedalRelationDTO> findUserMedalPage(PageParams pageParams, String userId, String categoryId){
		Map<String, Object> conditions=new HashMap<String, Object>();
		conditions.put("userId", userId);
		conditions.put("categoryId", categoryId);
		Pagination<UserMedalRelationDTO> queryPgn = new Pagination<UserMedalRelationDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total =userMedalRelationService.findUserItemRelaPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total > 0){
			queryPgn.setTotal(total);
			List<UserMedalRelationDTO> list=userMedalRelationService.findUserItemRelaPageList(queryPgn);
			queryPgn.setList(list);
		}
		return queryPgn;
	}

	@Override
	public List<RespUserMedal> findMedalByUserId(String userId) {
		return userMedalItemMapper.findMedalByUserId(userId);
	}

	@Override
	public UserMedalItem selectByCode(String code) {
		return userMedalItemMapper.selectByCode(code);
	}

}